#!/bin/tcsh -fex
####
#
#############################################################################
#                                                                           #
# Title: Import 2D Image                                                    #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 09/07/2017                                             #
# Last Modification: 09/07/2017                                             #
# Author...........: focus-em.org                                           #
#                                                                           #
#############################################################################
#
# SORTORDER: 40
#
# MANUAL: <B>Welcome to the File Importer</B>
#
# DISPLAY: Calc_from_sample_pixel
# DISPLAY: comment
# DISPLAY: crop_histogram
# DISPLAY: crop_histogram_stdev
# DISPLAY: CS
# DISPLAY: imagename
# DISPLAY: imagename_original
# DISPLAY: imagenumber
# DISPLAY: import_bin
# DISPLAY: import_bin_target
# DISPLAY: import_rawstack
# DISPLAY: import_rawstack_original
# DISPLAY: import_rawstack_type
# DISPLAY: KV
# DISPLAY: magnification
# DISPLAY: movie_imagenumber_total
# DISPLAY: movie_stackname
# DISPLAY: movie_stackname_original
# DISPLAY: orig_sample_pixel
# DISPLAY: sample_pixel
# DISPLAY: SERIALEM_DEFAULT_FACTOR
# DISPLAY: SERIALEM_FACTOR
# DISPLAY: stepdigitizer
# DISPLAY: take_header_orig_sample_pixel
# DISPLAY: tempkeep
# DISPLAY: Thread_Number
#
#$end_local_vars
#
# Static directory variables at disposition are:
# appDir_2dx
# scripts-standard_2dx
# scripts-custom_2dx
#
set bin_2dx = ""
set proc_2dx = ""
set app_2dx_mrc_converter = ""
#
# Variables to be available from the config file are:
set Calc_from_sample_pixel = ""
set crop_histogram = ""
set crop_histogram_stdev = ""
set CS = ""
set defocus_phase_shift_doit = ""
set export_basedir = ""
set export_rawstack_subdir = ""
set imagename = ""
set imagename_original = ""
set import_bin = ""
set import_bin_target = ""
set import_original_time = ""
set import_rawstack = ""
set import_rawstack_original = ""
set import_rawstack_type = ""
set KV = ""
set magnification = ""
set movie_imagenumber_total = ""
set movie_stackname = ""
set nonmaskimagename = ""
set orig_sample_pixel = ""
set sample_pixel = ""
set SERIALEM_DEFAULT_FACTOR = ""
set SERIALEM_FACTOR = ""
set stepdigitizer = ""
set take_header_orig_sample_pixel = ""
set tempkeep = ""
set Thread_Number = ""
#
#$end_vars
#
set scriptname = import_image
#
\rm -f LOGS/${scriptname}.results
#
source ${proc_2dx}/initialize
#
source ${proc_2dx}/2dx_makedirs
#
set import_produce_gainref2D = "y"
set import_produce_gainref2Dfft = "y"
#
setenv OMP_NUM_THREADS ${Thread_Number}
#
if ( ${website_blur_doit} == "y" ) then
  set blurkernel = ${website_blurkernel}
else
  set blurkernel = 0
endif
#
echo "<<@progress: 10>>"
#
if ( ${CS} == "ScriptWillPutNumberHere" ) then
  set CS = ${Default_CS}
  echo "set CS = ${CS}" >> LOGS/${scriptname}.results
endif
#
if ( ${KV} == "ScriptWillPutNumberHere" ) then
  set KV = ${Default_KV}
  echo "set KV = ${KV}" >> LOGS/${scriptname}.results
endif
#
if ( ${defocus_phase_shift_doit} == "-" ) then
  set defocus_phase_shift_doit = ${Default_phase_shift_doit}
  echo "set defocus_phase_shift_doit = ${defocus_phase_shift_doit}" >> LOGS/${scriptname}.results
endif
#
if(${import_original_time} == "-" || ${import_original_time} == "") then
  @ status_date = `date +%s` * 1000
  set date_text = "Processed at "`echo ${status_date} | awk '{ s = $1 / 1000 } END { print s }' | ${app_gawk} '{print strftime("%c", $0)}' `
  # set date_text = "Processed at "`echo ${status_date} | awk '{ s = $1 / 1000 } END { print s }' | awk 'BEGIN{m=split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",d,"|") for(o=1;o<=m;o++){ months[d[o]]=sprintf("%02d",o)} format = "%m/%d/%Y %H:%M" } {split($4,time,":") date = (strftime("%Y") " " months[$2] " " $3 " " time[1] " " time[2] " 0") print strftime(format, mktime(date))}'`
  echo "set import_original_time = ${status_date}" >> LOGS/${scriptname}.results
else
  set status_date = ${import_original_time}
  set date_text = "Recorded at "`echo ${status_date} | awk '{ s = $1 / 1000 } END { print s }' | ${app_gawk} '{print strftime("%c", $0)}' `
  # set date_text = "Recorded at "`echo ${status_date} | awk '{ s = $1 / 1000 } END { print s }' | awk 'BEGIN{m=split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",d,"|") for(o=1;o<=m;o++){ months[d[o]]=sprintf("%02d",o)} format = "%m/%d/%Y %H:%M" } {split($4,time,":") date = (strftime("%Y") " " months[$2] " " $3 " " time[1] " " time[2] " 0") print strftime(format, mktime(date))}'`
endif
#
#-------------------------------------------------------------------------------------------
#
if ( ${imagename} == "ScriptWillPutNameHere" ) then
	set imagename = "raw_gaincorrectedstack"
	echo "set imagename = ${imagename}" >> LOGS/${scriptname}.results
endif
set movie_stackname = ${imagename}
echo "set movie_stackname = ${movie_stackname}" >> LOGS/${scriptname}.results
set raw_gaincorrectedstack = ${imagename}
echo "set raw_gaincorrectedstack = ${raw_gaincorrectedstack}" >> LOGS/${scriptname}.results
#
if ( ! ( -e ${imagename}.mrc || -e ${imagename}.tif || -e ${imagename}.tiff ) ) then
  if ( x${imagename_original}x != "x-x" ) then
    if ( -e ${imagename_original} ) then
      set namestub = `echo ${imagename_original} | sed -e 's/\.mrc//g' -e 's/\.tif//g' | sed -e 's/\./_/g' -e 's/\,/_/g' -e 's/ /_/g' -e's/:/_/g' -e's/#/_/g'`
      set namestub_end = `echo ${namestub} | rev | cut -c1-4 | rev`
      rsync -auvP ${imagename_original} ${imagename}.${namestub_end}
    endif
  else
      set namestub = `echo ${imagename} | sed -e 's/\.mrc//g' -e 's/\.tif//g' | sed -e 's/\./_/g' -e 's/\,/_/g' -e 's/ /_/g' -e's/:/_/g' -e's/#/_/g'`
      set namestub_end = `echo ${namestub} | rev | cut -c1-4 | rev`
  endif
endif
#
if ( -e ${imagename}.tif ) then
  set imagename_end = tif
else if ( -e ${imagename}.tiff ) then
  set imagename_end = tiff
else if ( -e ${imagename}.mrc ) then
  set imagename_end = mrc
else if ( -e ${imagename}.mrcs ) then
  set imagename_end = mrcs
endif
#
#-------------------------------------------------------------------------------------------
#
if ( -e ${imagename}_raw.${imagename_end} ) then
  \cp -f ${imagename}_raw.${imagename_end} ${imagename}.${imagename_end}
else
  \cp -f ${imagename}.${imagename_end} ${imagename}_raw.${imagename_end}
endif
#
echo "# IMAGE-IMPORTANT: ${imagename}_raw.${imagename_end} <Input image (raw)>" >> LOGS/${scriptname}.results
#
#-------------------------------------------------------------------------------------------
#
if ( ${imagename_end} == "mrc" ) then
  set image_type = 1
else if ( ${imagename_end} == "tif" ) then
  set image_type = 2
  #################################################################################
  ${proc_2dx}/linblock "Running: clip unpack ${imagename}.tif ${imagename}.mrc"
  #################################################################################
  \rm -f ${imagename}.mrc
  time nice ${dir_imod}/bin/clip unpack -m 2 ${imagename}.tif ${imagename}.mrc
  echo "# IMAGE-IMPORTANT: ${imagename}.tif <Input image (TIFF)>" >> LOGS/${scriptname}.results
else if ( ${imagename_end} == "tiff" ) then
  set image_type = 3
  #################################################################################
  ${proc_2dx}/linblock "Running: clip unpack ${imagename}.tiff ${imagename}.mrc"
  #################################################################################
  \rm -f ${imagename}.mrc
  time nice ${dir_imod}/bin/clip unpack -m 2 ${imagename}.tiff ${imagename}.mrc
  echo "# IMAGE-IMPORTANT: ${imagename}.tiff <Input image (TIFF)>" >> LOGS/${scriptname}.results
else if ( ${imagename_end} == "mrcs" ) then
  set image_type = 1
else
  ${proc_2dx}/protest "ERROR: file format ${imagename_end} not supported."
endif
# 
echo "# IMAGE-IMPORTANT: ${imagename}.mrc <Input image (MRC)>" >> LOGS/${scriptname}.results
#
echo "<<@progress: 20>>"
#
#
if ( ${take_header_orig_sample_pixel} == "y" ) then
# Here we read the header to get the original pixel size, and set sample_pixel for the first time.
  #
  set sample_pixel = `${dir_imod}/bin/clip info ${imagename}.mrc | grep Scale | cut -d\( -f2 | cut -dx -f1`
  #
  set movie_imagenumber_total = 1
  echo "set movie_imagenumber_total = ${movie_imagenumber_total}" >> LOGS/${scriptname}.results
  #
  set orig_sample_pixel = ${sample_pixel}
else
# Or, the user provides the orig_sample_pixel and this becomes our initial sample_pixel.
  set sample_pixel = ${orig_sample_pixel}
endif
#
if ( ${Calc_from_sample_pixel} == "y" ) then
  set magnification = `echo "scale=3; ${stepdigitizer} * 10000 / ${sample_pixel}" | bc ` 
  echo "set magnification = ${magnification}"  >> LOGS/${scriptname}.results
endif
#
echo "<<@progress: 50>>"
#
echo "set sample_pixel = ${sample_pixel}"  >> LOGS/${scriptname}.results
#
echo "<<@evaluate>>"
#
if ( ${crop_histogram} == "y" ) then
  set val_avg = `${dir_imod}/bin/clip stats ${imagename}.mrc | head -n 3 | tail -n 1 | cut -d\)  -f3 | awk '{print $1}'`
  set val_stddev = `${dir_imod}/bin/clip stats ${imagename}.mrc | head -n 3 | tail -n 1 | cut -d\)  -f3 | awk '{print $2}'`
  set newmin = `echo "scale=6; ${val_avg} - ${val_stddev} * ${crop_histogram_stdev}" | bc ` 
  set newmax = `echo "scale=6; ${val_avg} + ${val_stddev} * ${crop_histogram_stdev}" | bc ` 
  echo ":Statistics on 2D images are:  Average = ${val_avg}, StdDev = ${val_stddev}"
  #################################################################################
  ${proc_2dx}/linblock "Truncating histogram of 2D average to ${newmin} ... ${newmax}"
  #################################################################################
  #
  time nice +19 ${dir_imod}/bin/clip truncate -l ${newmin} -h ${newmax} ${imagename}.mrc ${imagename}_trunc.mrc
  \mv -f ${imagename}_trunc.mrc ${imagename}.mrc
endif
#
\rm -f tmp.png
\rm -f tmp2.png
\rm -f STATUS/1-image.jpg
${app_2dx_mrc_converter} --size 400 ${imagename}.mrc tmp.png 
${app_python} ${proc_2dx}/PNGannotator.py tmp.png tmp2.png 10 350 0 "Raw average"
${app_python} ${proc_2dx}/PNGannotator.py tmp2.png ${imagename}.mrc.png 10 365 0 "${date_text}"
if ( ${blurkernel} != "0" ) then
  ${app_python} ${proc_2dx}/PNGannotator.py tmp.png tmp2.png 135 180 ${blurkernel} "I N T E N T I O N A L L Y"
  ${app_python} ${proc_2dx}/PNGannotator.py tmp2.png tmp.png 135 195 0             "   B  L  U  R  R  E  D"
endif
${app_python} ${proc_2dx}/PNGannotator.py tmp.png tmp2.png 10 350 0 "Raw average"
${app_python} ${proc_2dx}/PNGannotator.py tmp2.png STATUS/1-image.jpg 10 365 0 "${date_text}"
\rm -f tmp.png 
\rm -f tmp2.png
#
  #################################################################################
  ${proc_2dx}/linblock "Running: labelh.exe to normalize MRC image"
  #################################################################################
  # 
  echo "<<@progress: 75>>"
  #
  \rm -f FASTDISK/${imagename}_norm.mrc
  #
  time ${bin_2dx}/labelh.exe << eot
${imagename}.mrc
42
FASTDISK/${imagename}_norm.mrc
eot
  #
  ${proc_2dx}/linblock "Running: clip fft FASTDISK/${imagename}_norm.mrc ${imagename}_fft.mrc"
  time nice +19 ${dir_imod}/bin/clip fft FASTDISK/${imagename}_norm.mrc ${imagename}_fft.mrc
  echo "# IMAGE-IMPORTANT: ${imagename}_fft.mrc <Image FFT (2D)>" >> LOGS/${scriptname}.results
  \rm -f FASTDISK/${imagename}_norm.mrc
  \rm -f tmp.png
  \rm -f tmp2.png
  \rm -f STATUS/1-FFT.jpg
  ${app_2dx_mrc_converter} --size 400 ${imagename}_fft.mrc tmp.png
  ${app_python} ${proc_2dx}/PNGannotator.py tmp.png tmp2.png 10 350 0 "FFT of raw average"
  ${app_python} ${proc_2dx}/PNGannotator.py tmp2.png ${imagename}_fft.mrc.png 10 365 0 "${date_text}"
  ${app_python} ${proc_2dx}/PNGannotator.py tmp2.png STATUS/1-FFT.jpg 10 365 0 "${date_text}"
  \rm -f tmp.png
  \rm -f tmp2.png
endif
#
# This is needed for MotionCor2 later:
echo "set do_unpack = y" >> LOGS/${scriptname}.results
# 
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
echo "<<@progress: 100>>"
echo "<<@evaluate>>"
#
exit
#
